'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 主城小玩法
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var _ = require('lodash');
var dataApi = require('../libs/dataApi.js');
var moment = require('moment');
var huntMgr = require('../mgr/huntMgr.js');
var constantUtil = require('../util/constantUtil.js');
var smallPlayConf = require('../config/smallPlayConf.js');

var huntItemSchema = new mongoose.Schema({
    // hunt类型
    hunt_type: {
        type: Number
    }
});

if (!huntItemSchema.options.toJSON) {
    huntItemSchema.options.toJSON = {};
}
/* jshint unused:false */
huntItemSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.__v;
};
//////////////////////////////////////////////////////////////////////////////////
var _now_time = function () {
    return moment().unix();
};
/**
 * 主城小玩法
 * @type {mongoose}
 */
var huntSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 主城宝箱
    chest: {
        type: [Number]
    },

    // 主城宝箱刷新时间
    chest_time: {
        type: Date,
        default: _now_time
    },

    // 外城猎物
    hunt: [huntItemSchema],

    // 外城猎物刷新时间
    hunt_time: {
        type: Date,
        default: _now_time
    }
});

huntSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 设置主城宝箱
 */
huntSchema.methods.setChest = function () {
    this.chest = [];
    this.chest.push(1);
    this.chest_time = moment();
};

/**
 * 获取主城宝箱
 */
huntSchema.methods.getChest = function () {
    return this.chest;
};

/**
 * 获取主城宝箱
 */
huntSchema.methods.isChest = function () {
    return _.indexOf(this.chest, 1) !== -1;
};

/**
 * 获取猎物列表
 */
huntSchema.methods.getHuntList = function () {
    return this.hunt;
};

/**
 * 设置外城猎物
 */
huntSchema.methods.setHunt = function (hunt_id) {
    if (this.getHuntList().length < constantUtil.value('hunt_monster_limit')) {
        this.pushHunt(hunt_id);
        this.hunt_time = moment();
        return true;
    } else {
        return false;
    }
};

/**
 * 加入猎物
 */
huntSchema.methods.pushHunt = function (hunt_id) {
    this.hunt.push({
        hunt_type: hunt_id
    });
};

/**
 * 移除被击杀猎物
 */
huntSchema.methods.removeHunt = function (hunt_id) {
    var item = {};
    var isVaild = false;
    _.some(this.getHuntList(), function (value) {
        if (value.hunt_type == hunt_id) {
            item = value;
            isVaild = true;
            return true;
        }
    });
    if (!!isVaild) {
        item.remove();
    }
    return isVaild;
};

/**
 * 移除被击杀猎物
 */
huntSchema.methods.removeChest = function () {
    this.chest = [];
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
huntSchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
huntSchema.methods.push = function () {
    this.pushModify();
};

huntSchema.methods.pushModify = function () {
    huntMgr.sendHuntChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send vip message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!huntSchema.options.toJSON) {
    huntSchema.options.toJSON = {};
}

/* jshint unused:false */
huntSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
};
mongoose.model('Hunt', huntSchema);
